package com.unittec.drainage.provider.manage.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.unittec.drainage.provider.log.Log;
import com.unittec.drainage.provider.manage.service.RegionService;
import com.unittec.drainage.provider.utils.ValidationUtils;
import com.unittec.drainage.sdk.log.enums.LogOperateEnum;
import com.unittec.drainage.sdk.log.enums.LogStatusEnum;
import com.unittec.drainage.sdk.manage.vo.RegionTreeVo;
import com.unittec.drainage.sdk.manage.request.*;
import com.unittec.drainage.sdk.manage.response.*;
import com.unittec.drainage.sdk.manage.vo.RegionVo;
import com.unittec.drainage.sdk.root.domain.GatewayRequest;
import com.unittec.drainage.sdk.root.domain.Result;
import com.unittec.drainage.sdk.root.enums.BizCode;
import com.unittec.drainage.sdk.root.enums.BizCodeEnum;
import com.unittec.drainage.sdk.root.enums.ErrorEnum;
import com.unittec.drainage.sdk.root.internal.exception.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 区域管理
 *
 * @author Jason
 * @date 2021/8/6
 */
@RestController
@Api(tags = "区域管理")
public class RegionController {

    @Autowired
    private Log log;

    @Autowired
    private RegionService regionService;

    @RequestMapping(value = "/301000", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "区域树形列表", notes = "区域树形列表")
    public Result<Res301000> regionTreeList(@RequestBody GatewayRequest<Req301000> req) {
        log.debug(req, "regionTreeList  {} ,param {}", "begin", JSON.toJSONString(req.getContent()));
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res301000 res301000 = null;

        try {
            ValidationUtils.validateEntityStr(req.getContent());
            List<RegionTreeVo> list = regionService.queryTreeList(req.getContent().getType());
            res301000 = new Res301000();
            res301000.setList(list);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            if (e instanceof BusinessException) {
                return Result.error(req, e.getMessage());
            } else {
                return Result.error(req, ErrorEnum.QUERY_ERROR.getErrorMessage());
            }
        }
        log.debug(req, "regionTreeList {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res301000);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/301001", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "区域列表", notes = "区域列表")
    public Result<Res301001> regionList(@RequestBody GatewayRequest<Req301001> req) {
        log.debug(req, "regionList  {} ,param {}", "begin", JSON.toJSONString(req.getContent()));
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res301001 res301001 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            List<RegionVo> list = regionService.regionList(req.getContent());
            res301001 = new Res301001();
            res301001.setList(list);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            if (e instanceof BusinessException) {
                return Result.error(req, e.getMessage());
            } else {
                return Result.error(req, ErrorEnum.QUERY_ERROR.getErrorMessage());
            }
        }
        log.debug(req, "regionList {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res301001);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/301002", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "添加区域", notes = "添加区域")
    public Result<Res301002> addRegion(@RequestBody GatewayRequest<Req301002> req) {
        log.debug(req, "addRegion {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res301002 res301002 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            regionService.addRegion(req.getContent());
            res301002 = new Res301002();
            res301002.setIsSuccess(true);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("301002", JSON.toJSONString(req), "添加区域", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "addRegion {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("301002", JSON.toJSONString(req), "添加区域", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res301002);
        } else {
            log.addOperateLog("301002", JSON.toJSONString(req), "添加区域", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/301003", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改区域属性", notes = "修改区域属性")
    public Result<Res301003> updateRegionAttribute(@RequestBody GatewayRequest<Req301003> req) {
        log.debug(req, "updateRegionAttribute {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res301003 res301003 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            regionService.updateRegionAttribute(req.getContent());
            res301003 = new Res301003();
            res301003.setIsSuccess(true);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("301003", JSON.toJSONString(req), "修改区域属性", log.buildErrmsg(e),
                    LogOperateEnum.LOG_UPDATE.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "updateRegionAttribute {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("301003", JSON.toJSONString(req), "修改区域属性", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_UPDATE.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res301003);
        } else {
            log.addOperateLog("301003", JSON.toJSONString(req), "修改区域属性", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_UPDATE.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/301004", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改区域坐标", notes = "修改区域坐标")
    public Result<Res301004> updateRegionCoordinates(@RequestBody GatewayRequest<Req301004> req) {
        log.debug(req, "updateRegionCoordinates {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res301004 res301004 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            regionService.updateRegionCoordinates(req.getContent());
            res301004 = new Res301004();
            res301004.setIsSuccess(true);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("301004", JSON.toJSONString(req), "修改区域坐标", log.buildErrmsg(e),
                    LogOperateEnum.LOG_UPDATE.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "updateRegionCoordinates {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("301004", JSON.toJSONString(req), "修改区域坐标", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_UPDATE.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res301004);
        } else {
            log.addOperateLog("301004", JSON.toJSONString(req), "修改区域坐标", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_UPDATE.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/301005", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "删除区域", notes = "删除区域")
    public Result<Res301005> deleteRegion(@RequestBody GatewayRequest<Req301005> req) {
        log.debug(req, "deleteRegion {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res301005 res301005 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            // 判断区域是否有下级
            // TODO: 2021/8/9  
            // 判断区域是否被用户使用
            // TODO: 2021/8/9
            regionService.deleteRegion(req.getContent());
            res301005 = new Res301005();
            res301005.setIsSuccess(true);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("301005", JSON.toJSONString(req), "删除区域", log.buildErrmsg(e),
                    LogOperateEnum.LOG_DEL.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "deleteRegion {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("301005", JSON.toJSONString(req), "删除区域", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_DEL.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res301005);
        } else {
            log.addOperateLog("301005", JSON.toJSONString(req), "删除区域", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_DEL.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/301006", method = RequestMethod.POST)
    @ApiOperation(value = "区域详情", notes = "区域详情")
    @ResponseBody
    public Result<Res301006> getRegionDetails(@RequestBody GatewayRequest<Req301006> req) {
        log.debug(req, "getRegionDetails {}  ", "begin");
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res301006 res301006 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            RegionVo regionVo = regionService.getRegionDetails(req.getContent().getId());
            res301006 = new Res301006();
            res301006.setRegionVo(regionVo);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "getRegionDetails {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res301006);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

}
